package com.sunagy.demand.backend.service.impl;

import com.sunagy.demand.backend.VO.request.AdminUserAddRequestVO;
import com.sunagy.demand.backend.entity.AdminUser;
import com.sunagy.demand.backend.VO.response.AdminUserResponseVO;
import com.sunagy.demand.backend.exception.AdminUserIncorrectPasswordException;
import com.sunagy.demand.backend.exception.AdminUserNotFoundException;
import com.sunagy.demand.backend.exception.UsernameExistException;
import com.sunagy.demand.backend.repository.AdminUserRepository;
import com.sunagy.demand.backend.service.AdminUserService;
import com.sunagy.demand.backend.utils.BeanUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * desc：#TODO fill me ！
 * author：三餘無夢生
 * date：2018/4/24
 * time：下午6:25
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired
    private AdminUserRepository adminUserRepository;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public AdminUserResponseVO login(String username, String password) {
        AdminUser adminUser = adminUserRepository.findByUsername(username);
        if (adminUser == null){
            throw new AdminUserNotFoundException();
        }
        if(!passwordEncoder.matches(password, adminUser.getPassword())){
            throw new AdminUserIncorrectPasswordException();
        }
        AdminUserResponseVO adminUserResponseVO = new AdminUserResponseVO();
        BeanUtil.copyProperties(adminUserResponseVO,adminUser);
//        adminUserResponseVO.setId(adminUser.getId());
//        adminUserResponseVO.setUsername(adminUser.getUsername());
        return adminUserResponseVO;
    }

    @Override
    @Transactional
    public AdminUserResponseVO add(AdminUserAddRequestVO adminUserAddRequestVO) {
        if (adminUserRepository.findByUsername(adminUserAddRequestVO.getUsername()) != null){
            throw new UsernameExistException();
        }
        adminUserAddRequestVO.setPassword(passwordEncoder.encode(adminUserAddRequestVO.getPassword()));
        AdminUser adminUser = new AdminUser();
        BeanUtil.copyProperties(adminUser,adminUserAddRequestVO);
        adminUser = adminUserRepository.save(adminUser);
        AdminUserResponseVO adminUserResponseVO = new AdminUserResponseVO();
        adminUserResponseVO.setId(adminUser.getId());
        adminUserResponseVO.setUsername(adminUser.getUsername());
        return adminUserResponseVO;
    }

    @Override
    public List<AdminUserResponseVO> getAll() {
        List<AdminUserResponseVO> adminUserDtoList = new ArrayList<>();
        adminUserDtoList = (List<AdminUserResponseVO>) BeanUtil.copyPropertiesList(adminUserRepository.findAll(),AdminUserResponseVO.class);
        return adminUserDtoList;
    }

    @Override
    @Transactional
    public AdminUserResponseVO delete(Integer id) {
        AdminUserResponseVO adminUserDto = new AdminUserResponseVO();
        AdminUser adminUser = adminUserRepository.findOne(id);
        adminUser.setEnable(false);
        BeanUtils.copyProperties(adminUserRepository.save(adminUser),adminUserDto);
        return adminUserDto;
    }

    @Override
    @Transactional
    public AdminUserResponseVO edit(Integer id, AdminUser adminUser) {
        AdminUserResponseVO adminUserResponseVO = new AdminUserResponseVO();
        AdminUser a = adminUserRepository.findOne(id);
        String password = passwordEncoder.encode(adminUser.getPassword());
        adminUser.setPassword(password);
        BeanUtil.copyProperties(a,adminUser);
        BeanUtil.copyProperties(adminUserResponseVO,adminUserRepository.save(a));
        return adminUserResponseVO;
    }

}
